package com.hfzy.ihk.web.userCenter.controller;


import com.hfzy.ihk.facade.userCenter.entity.Role;
import com.hfzy.ihk.web.oauth.client.table.PageTableHandler;
import com.hfzy.ihk.web.oauth.client.table.PageTableRequest;
import com.hfzy.ihk.web.oauth.client.table.PageTableResponse;
import com.hfzy.ihk.web.userCenter.biz.RoleBiz;
import com.hfzy.ihk.web.userCenter.mapper.RoleMapper;
import com.hfzy.ihk.web.userCenter.vo.RoleDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用于集成spring security 前端控制器
 * </p>
 *
 * @author chenzesen
 * @since 2018-12-28
 */
@RestController
@RequestMapping("role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleMapper roleMapper ;


    @Autowired
    RoleBiz roleBiz;

    @RequestMapping("roleList")
    public ModelAndView saveUser(ModelAndView view) {
        view.setViewName("page/role/roleList");
        return view;
    }

    @RequestMapping("addRole")
    public ModelAndView allRole(ModelAndView view) {
        view.setViewName("page/role/addRole");
        return view;
    }


    @RequestMapping("editRole")
    public ModelAndView editRole(ModelAndView view,Long id) {
        view.setViewName("page/role/editRole");
        Map<String,Object> map = new HashMap<>();
        Role role = roleMapper.selectById(id);
        view.addObject("role",role);
        view.addObject("id",id);
        List<String> userPrivList = roleBiz.getRoleHasPrivByRole(id);
        view.addObject("userPrivList",userPrivList);
        return view;
    }


    /**
     * 查询列表
     * @param request
     * @return
     */
    @RequestMapping("queryRoles")
    public PageTableResponse listRoles(PageTableRequest request) {
            request.setOffset((request.getOffset()-1)*request.getLimit());
        return new PageTableHandler(new PageTableHandler.CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return roleMapper.count(request.getParams());
            }
        }, new PageTableHandler.ListHandler() {

            @Override
            public List<Role> list(PageTableRequest request) {
                List<Role> list = roleMapper.list(request.getParams(), request.getOffset(), request.getLimit());
                return list;
            }
        }).handle(request);
    }


    /**
     * 保存权限
     * @param roleDto
     * @return
     */
    @RequestMapping("saveRole")
    public Map<String,Object> saveRole( RoleDto roleDto) {
        Map<String,Object> map = new HashMap<>();
        try {
            roleBiz.saveRole(roleDto);
            map.put("code", 1);
            map.put("msg", "保存成功");
        }catch (IllegalArgumentException e1){
            map.put("code",0);
            map.put("msg",e1.getMessage());

        }catch (Exception e){
            map.put("code",0);
            map.put("msg","保存失败");

        }
        return map;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("delete")
    public Map<String,Object> delete( Long id) {
        Map<String,Object> map = new HashMap<>();
        try {
            roleBiz.deleteRole(id);
            map.put("code",1);
            map.put("msg","删除成功");
        }catch (Exception e){
            log.error("删除权限异常~",e);
            map.put("code",1);
            map.put("msg","删除成功");
        }
        return map;
    }

    @RequestMapping("getAllRole")
    public List<Role> getAllRole(){
        return roleMapper.list(new HashMap<String,Object>() {
        }, null, null);
    }

}
